/**
 * Created by cchen on 2015/4/17.
 */
'use strict';
angular.module("66MobileTopicApp",['ngRoute', 'ngResource','mobile.topic.controller','mobile.topic1.controller','mobile.topic.services'])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/topic/:topicId/:token/:deviceId', {templateUrl: 'topic.html', controller: 'topicCtrl'});
        $routeProvider.when('/topic1/:topicId/:token/:deviceId', {templateUrl: 'topic1.html', controller: 'topic1Ctrl'});
        $routeProvider.when('/error', {templateUrl: 'error.html'});
        $routeProvider.otherwise({redirectTo: '/error'});
    }]);

angular.module('mobile.topic.controller', [])
    .controller('topicCtrl', function($scope,$routeParams,topicService){
        topicService.get($routeParams,function(res){
            // $scope.content=res.data.desc;
           var dese= $(res.data.desc);
            var imgs =[]
            dese.find("img").each(function(index,element){
                var ee=$(element)
                if(index>3){
                    imgs[index]=ee.attr("src")
                    ee.attr("src","");
                }

            });

            var content=$("#content");
            content.html(dese);
            console.log(window.outerHeight);
            console.log($(document).height() - $(window).height() <= $(window).scrollTop() + 1000);
            $(document).endlessScroll({
                bottomPixels:1000,
                fireDelay: 20,
                callback: function(i) {
                    $(content.find("img")[i+4]).attr("src",imgs[i]);
                    console.log(i);
                }
            });


        })
    });

angular.module('mobile.topic1.controller', [])
    .controller('topic1Ctrl', function($scope,$routeParams,topicService){
        topicService.get($routeParams,function(res){
            $("#content").html(res.data.desc);
        })
    });

angular.module('mobile.topic.services', ['ngResource']).
    factory('topicService', function ($resource) {
        return $resource('/rest/v1.0/Systems/getDetailOfSpecialTopic/:topicId/:token/:deviceId', {}, {});
    })